package com.ruoyi.project.shortUrl.config.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.shortUrl.config.domain.ShortConfig;
import com.ruoyi.project.shortUrl.config.enums.ShortUrlOpenWay;
import com.ruoyi.project.shortUrl.config.service.ShortConfigService;

/**
 * 短链接系统设置Controller
 * 
 * @author ruoyi
 * @date 2020-03-29
 */
@Controller
@RequestMapping("/short/config")
public class ShortConfigController extends BaseController
{
    private String prefix = "shortUrl/config";

    @Autowired
    private ShortConfigService shortConfigService;

    @RequiresPermissions("short:config:view")
    @GetMapping()
    public String config()
    {
        return prefix + "/config";
    }

    /**
     * 查询短链接系统设置列表
     */
    @RequiresPermissions("short:config:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShortConfig shortConfig)
    {
        startPage();
        List<ShortConfig> list = shortConfigService.getShortConfigList(shortConfig);
        for(ShortConfig c:list) {
        	if("number-day".equals(c.getType())) {
        		if(!"-1".equals(c.getValue())) {
        			c.setValue(c.getValue()+"天");
        		}else {
        			c.setValue("永久");
        		}
        	}
        	
        	if("number-size".equals(c.getType())) {
        		int size = -1;
        		try {
        			size = Integer.parseInt(c.getValue());
				} catch (NumberFormatException e) {
					size = -1;
				}
        		if(size<0) {
        			c.setValue("无限制");
        		}else {
        			c.setValue(size+"条");
        		}
        	}
        	
        	if( "select-frame".equals(c.getType()) ) {
        		c.setValue(ShortUrlOpenWay.getEnum(c.getValue()).getName());
        	}
        	
        	if( "yes-no".equals(c.getType())) {
        		c.setValue(Boolean.valueOf(c.getValue())?"开启":"关闭");
        	}
        }
        
        return getDataTable(list);
    }

    /**
     * 导出短链接系统设置列表
     */
    @RequiresPermissions("short:config:export")
    @Log(title = "短链接系统设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShortConfig shortConfig)
    {
        List<ShortConfig> list = shortConfigService.getShortConfigList(shortConfig);
        ExcelUtil<ShortConfig> util = new ExcelUtil<ShortConfig>(ShortConfig.class);
        return util.exportExcel(list, "config");
    }

    /**
     * 新增短链接系统设置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 修改短链接系统设置
     */
    @GetMapping("/edit/{key}")
    public String edit(@PathVariable("key") String key, ModelMap mmap)
    {
        ShortConfig shortConfig = shortConfigService.getConfig(key);
        mmap.put("shortConfig", shortConfig);
        return prefix + "/edit";
    }

    /**
     * 修改保存短链接系统设置
     */
    @RequiresPermissions("short:config:edit")
    @Log(title = "短链接系统设置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ShortConfig shortConfig)
    {
        return toAjax(shortConfigService.updateConfig(shortConfig.getKey(), shortConfig.getValue()));
    }
    
}
